<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head></head>
<link th:href="@{/css/login.css}" rel="stylesheet"/>
<body class="pace-top">
<div id="page-loader" class="fade in"><span class="spinner"></span></div>
<div class="container_login">
    <img class="bg_image" src="img/bj.png" alt="">
    <div style="float: left;">
        <img class="logo" src="img/logo.jpg" alt="" style="">
        <p  class="words" >Sapling OS </p>
        <form th:action="@{/login}" th:object="${loginForm}" id="s" method="POST" class="margin-bottom-0">
            <span style="color: red" th:text="${msg}"></span>
            <input class="text" type="text" placeholder="账号" id="username" name="username" >
            <input class="text" type="password" placeholder="密码" id="password" name="password">
            <input id="ck_rmbUser" class="checkbox" type="checkbox"><span class="remember">记住密码</span>
            <input class="sub" id="sub" type="submit" value="登录">
        </form>
        <p class="hr"></p>
        <p class="copyright">Copyright © 2017 苗木. 版权所有 </p>
    </div>
</div>
<script>
    window.onload = function(){
        var oForm = document.getElementById('s');
        var oUser = document.getElementById('username');
        var oPswd = document.getElementById('password');
        var oRemember = document.getElementById('ck_rmbUser');
        //页面初始化时，如果帐号密码cookie存在则填充
        if(getCookie('user') && getCookie('pswd')){
            oUser.value = getCookie('user');
            oPswd.value = getCookie('pswd');
            oRemember.checked = true;
        }
        //复选框勾选状态发生改变时，如果未勾选则清除cookie
        oRemember.onchange = function(){
            if(!this.checked){
                delCookie('user');
                delCookie('pswd');
            }
        };
        //表单提交事件触发时，如果复选框是勾选状态则保存cookie
        oForm.onsubmit = function(){
            if(remember.checked){
                setCookie('user',oUser.value,7); //保存帐号到cookie，有效期7天
                setCookie('pswd',oPswd.value,7); //保存密码到cookie，有效期7天
            }
        };
    };
    //设置cookie
    function setCookie(name,value,day){
        var date = new Date();
        date.setDate(date.getDate() + day);
        document.cookie = name + '=' + value + ';expires='+ date;
    };
    //获取cookie
    function getCookie(name){
        var reg = RegExp(name+'=([^;]+)');
        var arr = document.cookie.match(reg);
        if(arr){
            return arr[1];
        }else{
            return '';
        }
    };
    //删除cookie
    function delCookie(name){
        setCookie(name,null,-1);
    };
</script>
</body>
</html>